package com.hksj.Intelligentmanagement.common;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * 统一响应体
 * @param <T> data指代的类型
 */
@Data
public class Rest<T> {
    @ApiModelProperty(value = "服务器返回状态码", name = "code", example = "200", required = true)
    private Integer code;

    @ApiModelProperty(value = "服务状态码中文说明", name = "msg", example = "成功", required = true)
    private String msg;
 
    private T data;
    @ApiModelProperty(value = "请求id", name = "requestId", example = "114514", required = true)
    private String requestId;
 
    @ApiModelProperty(value = "请求时间", name = "requestTime", example = "2023-05-25 00:00:00", required = true)
    private String requestTime;

    public Rest(){

    }

    public Rest(Integer code, String msg, String requestId, String requestTime, T o) {
        this.code = code;
        this.msg = msg;
        this.data = o;
        this.requestTime = requestTime;
        this.requestId = requestId;
    }
 
    public static Rest ok() {
        return ok("操作成功");
    }

    public static <T> Rest <T> ok(T data) {
        return new Rest<>(200, "操作成功", UUID.randomUUID().toString().replace("-", ""), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), data);
    }
 
    public static <T> Rest <T> ok(StatusCode statusCode) {
        return new Rest<>(statusCode.getCode(), statusCode.getMsg(), UUID.randomUUID().toString().replace("-", ""), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), null);
    }
 
    public static <T> Rest <T> fail(int code, String msg) {
        return new Rest<>(code, msg, UUID.randomUUID().toString().replace("-", ""), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), null);
    }

    public static <T> Rest <T> fail(String msg) {
        return new Rest<>(Constants.RESULT_CODE_SERVER_ERROR, msg, UUID.randomUUID().toString().replace("-", ""), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), null);
    }
 
    public static <T> Rest <T> fail(StatusCode statusCode) {
        return new Rest<>(statusCode.getCode(), statusCode.getMsg(), UUID.randomUUID().toString().replace("-", ""), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), null);
    }

    @Data
    public static class StatusCode {

        //状态码
        private Integer code;

        //提示信息
        private String msg;
    }
 
}